package com.learn.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.learn.bean.BatteryPackWithWindowName;
import com.learn.bean.ToKafkaSinkPojo;
import com.learn.util.FlinkSinkUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.kafka.clients.consumer.ConsumerRecord;

public class flinkDemoV2 {
    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        SingleOutputStreamOperator<BatteryPackWithWindowName> streamOperator = env
                .readTextFile("src/main/input/test.json")
                .map(value -> {

                            JSONObject jsonObject = JSON.parseObject(value);

                            String energysyscode = jsonObject.getString("energysyscode");
                            Double voltage = jsonObject.getDouble("voltage");
                            Double current = jsonObject.getDouble("current");
                            Long soc = jsonObject.getLong("soc");
                            Double mileage = jsonObject.getDouble("mileage");
                            Long command = jsonObject.getLong("command");
                            Long sampletime = jsonObject.getLong("sampletime");
                            String company = jsonObject.getString("company");
                            String vin = jsonObject.getString("vin");

                            return new BatteryPackWithWindowName(
                                    company, //由于是aian的历史数据，这里直接写死
                                    vin,
                                    energysyscode,
                                    voltage,
                                    current,
                                    soc,
                                    mileage,
                                    command,
                                    sampletime,
                                    ""
                            );
                        }
                );

        writeToKafka(streamOperator,"testV2");





        try {
            env.execute(flinkDemoV2.class.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private static void writeToKafka(SingleOutputStreamOperator<BatteryPackWithWindowName> streamOperator, String topic) {

        //todo 1、先封装thirdEtl这个流为ToKafkaSinkPojo这个pojo
        SingleOutputStreamOperator<ToKafkaSinkPojo> mapstream = streamOperator
                .filter(
                        bean->{
                            Long command = bean.getCommand();
                            return command!=null && command!=7 ;
                        })
                .map(
                        bean -> {

                            String key = bean.getVin();
                            String value = bean.toString();

                            return new ToKafkaSinkPojo(
                                    key,
                                    value
                            );
                        }
                );

        mapstream.addSink(FlinkSinkUtil.getKafkaSink(topic));
    }

    private static SingleOutputStreamOperator<BatteryPackWithWindowName> etlToPojo(DataStreamSource<ConsumerRecord<String, String>> dataStreamSource) {
        //todo 转换成pojo
        SingleOutputStreamOperator<BatteryPackWithWindowName> parsePojo = dataStreamSource
                .map(kafkaData -> {

                    String vin = kafkaData.key();
                    String value = kafkaData.value();

                    JSONObject jsonObject = JSON.parseObject(value);

                    String energysyscode = jsonObject.getString("energysyscode");
                    Double voltage = jsonObject.getDouble("voltage");
                    Double current = jsonObject.getDouble("current");
                    Long soc = jsonObject.getLong("soc");
                    Double mileage = jsonObject.getDouble("mileage");
                    Long command = jsonObject.getLong("command");
                    Long sampletime = jsonObject.getLong("sampletime");
                    String company = jsonObject.getString("company");

                    return new BatteryPackWithWindowName(
                            company, //由于是aian的历史数据，这里直接写死
                            vin,
                            energysyscode,
                            voltage,
                            current,
                            soc,
                            mileage,
                            command,
                            sampletime,
                            ""
                    );

                })
                //todo 这里只要cmd = 2的数据
                .filter(bean -> {
                    return bean.getCommand() == 2 || bean.getCommand() == 3;
                });
        return parsePojo;
    }


}
